﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.old {
    /**
     * （值类型参数、引用类型参数）
     */
    class ParametersDemo {
        public static void demo() {
            //test1();
            //test2();
            //test3();
            //test4();
            test5();
        }

        public static void test1() {
            Student8 stu = new Student8();
            int x = 8;
            stu.AddOne(x);
            Console.WriteLine(x);
        }

        public static void test2() {
            Student9 stu = new Student9() { Name = "Jack" };
            SomeMethod(stu);
            Console.WriteLine("{0}, {1}", stu.GetHashCode(), stu.Name);
        }

        public static void test3() {
            Student9 stu = new Student9() { Name = "Jack" };
            Console.WriteLine(">> {0}, {1}", stu.GetHashCode(), stu.Name);
            UpdateObject(stu);
            Console.WriteLine(">> {0}, {1}", stu.GetHashCode(), stu.Name);
        }

        public static void test4() {
            int y = 9;

            // 这里和Java区别很大 ; 使用 ref 修饰符显式指出 —— 此方法的副作用是改变实际参数的值
            IWantSideEffect(ref y);
            Console.WriteLine("y : {0}", y);
        }

        public static void test5() {
            Student9 oldStud = new Student9() { Name = "Jack" };
            Console.WriteLine("hashcode: {0}, name: {1}", oldStud.GetHashCode(), oldStud.Name);
            Console.WriteLine("------------");
            IWantSideEffect2(ref oldStud);
            Console.WriteLine("------------");
            Console.WriteLine("hashcode: {0}, name: {1}", oldStud.GetHashCode(), oldStud.Name);
        }

        static void UpdateObject(Student9 stu) {
            Console.WriteLine("{0}, {1}", stu.GetHashCode(), stu.Name);
            stu.Name = "Rose";
            Console.WriteLine("{0}, {1}", stu.GetHashCode(), stu.Name);
        }

        static void SomeMethod(Student9 stu) {
            stu = new Student9() { Name = "Tome" };
            Console.WriteLine("{0}, {1}", stu.GetHashCode(), stu.Name);
        }

        // 这里和Java区别很大 ; 使用 ref 修饰符显式指出 —— 此方法的副作用是改变实际参数的值
        static void IWantSideEffect(ref int x) {
            x = x + 100;
            Console.WriteLine("x : {0}", x);
        }

        static void IWantSideEffect2(ref Student9 stu) {
            stu = new Student9() { Name = "Tom" };
            Console.WriteLine("HashCode : {0}, Name : {1}", stu.GetHashCode(), stu.Name);
        }

    }

    class Student8 {
        // 这种情形下，方法内部会拷贝参数的副本，不会改变原来传入的变量的值
        public void AddOne(int x) {
            x = x + 1;
            Console.WriteLine(x);
        }
    }

    class Student9 {
        public string Name { get; set; }
    }
}
